/*
 * TempSensor.c
 *
 *  Created on: 31.10.2011
 *      Author: David
 */


#include "TempSensor.h"
#include "Tools.h"
#include "DS18x20_simpleFunc.h"
#include <util/delay.h>

u8 TemperatureBuffer[2] = {0, 0};

u8 GetTemperature(u8 *u8Temp, b8 *b8Sign)
{
	s16 s16TempTemper;
	if(initSensor() == 1)
	{
		writeSensor(0xCC); //skip ROM

		writeSensor(0x44); //convert T

		_delay_ms(500);
		_delay_ms(500);

		if(initSensor() == 1)
		{
			writeSensor(0xCC); //skip ROM

			writeSensor(0xBE); //read scratchpad

			readSensor();

			s16TempTemper = (((TemperatureBuffer[1] << 8) | TemperatureBuffer[0]) >> 1); //Rounded

			if(s16TempTemper < 0)
			{
				*b8Sign = bFALSE;
			}
			else
			{
				*b8Sign = bTRUE;
			}

			*u8Temp = (u8)U16_ABS_VALUE(s16TempTemper);

			return 1; // Reading of temperature was successful
		}
	}

	return 0;
}
